package com.scsj.supermarket.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.MineOrderBean;
import com.scsj.supermarket.bean.TotalOrderBean;
import com.scsj.supermarket.utils.GoPayBackWindow;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.utils.Tool;
import com.scsj.supermarket.view.activity.ordermodel.MyOrderActivity;
import com.scsj.supermarket.view.activity.ordermodel.OrderDetailActivity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单
 */

public class OrderAdapter extends BaseQuickAdapter<MineOrderBean.DataBean.ListBean, BaseViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private int orderState;
    private GoPayBackWindow goPayBackWindow;
    private View mainView;
    private WeakReference<MyOrderActivity> weak;
    //创建一个集合，用来存放获取到的所有的数据
    private List<MineOrderBean.DataBean.ListBean> shopBeanList= new ArrayList<>();
    //需要一个集合来存放每个店铺下的商品数据
    private List<MineOrderBean.DataBean.ListBean.ShopOrderGoodsDetailsListBean> goodsDetailsListBeans= new ArrayList<>();
    //还需要一个集合来存放获取到的所有商品的数据
    private List<MineOrderBean.DataBean.ListBean.ShopOrderGoodsDetailsListBean> AllGoodsDetailsListBeans= new ArrayList<>();
    public OrderAdapter(List data, Context context, int i) {
        super(R.layout.activity_order_list_layout, data);
        this.mContext = context;
        this.shopBeanList = data;
        this.mInflater = LayoutInflater.from(context);
        this.orderState = i;
        this.weak = new WeakReference<MyOrderActivity>((MyOrderActivity) context);
    }

    @Override
    protected void convert(BaseViewHolder helper, MineOrderBean.DataBean.ListBean item) {
        AllGoodsDetailsListBeans.clear();
        //对外层店铺集合进行循环
        for (int i=0;i<shopBeanList.size();i++){
            goodsDetailsListBeans= shopBeanList.get(i).getShopOrderGoodsDetailsList();
            AllGoodsDetailsListBeans.addAll(goodsDetailsListBeans);
            //接下来对各个控件进行赋值

            //店铺名字
            TextView shopNameTv= helper.getView(R.id.order_activity_shop_name_tv);
            String shopName= shopBeanList.get(i).getStoreName();
            shopNameTv.setText(shopName);

            //店铺总的商品数量
            TextView totalGoodsNumTv= helper.getView(R.id.order_activity_total_num_tv);
            String totalGoodsNum= String.valueOf(shopBeanList.get(i).getGoodsSum());
            totalGoodsNumTv.setText(totalGoodsNum);

            //店铺商品总价
            TextView totalGoodsPriceTv= helper.getView(R.id.order_activity_total_price_tv);
            String totalGoodsPrice= String.valueOf(shopBeanList.get(i).getGoodsAmount());
            totalGoodsPriceTv.setText(" "+totalGoodsPrice);
        }
        /**
         需要被添加的布局
         */
        LinearLayout addViewLayout = helper.getView(R.id.order_activity_add_view_layout);

        //添加之前先移除所有字View
        addViewLayout.removeAllViews();
        for (int i = 0; i < AllGoodsDetailsListBeans.size(); i++) {
            View requiredAddView = mInflater.inflate(R.layout.mine_order_add_item, null);

            //设置商品名称
            TextView goodsNameTv= requiredAddView.findViewById(R.id.goodNameTv);
            String goodsName= AllGoodsDetailsListBeans.get(i).getGoodsName();
            goodsNameTv.setText(goodsName);

            //设置商品单价
            TextView goodsPriceTv= requiredAddView.findViewById(R.id.goodPriceTv);
            String goodsPrice= String.valueOf(AllGoodsDetailsListBeans.get(i).getGoodsPrice());
            goodsPriceTv.setText(goodsPrice);

            //设置商品数量
            TextView goodsMountTv= requiredAddView.findViewById(R.id.goodNumTv);
            String goodsAmount= String.valueOf(AllGoodsDetailsListBeans.get(i).getBuyNum());
            goodsMountTv.setText(goodsAmount);
            addViewLayout.addView(requiredAddView);

            //设置商品logo
            ImageView goodsLogoIv= requiredAddView.findViewById(R.id.orderGoodIv);
            String imgUrl= (String) AllGoodsDetailsListBeans.get(i).getGoodsImage();
            Glide
                    .with(mContext)
                    .load(imgUrl)
                    .placeholder(R.mipmap.classify_goods_default)
                    .error(R.mipmap.classify_goods_default)
                    .into(goodsLogoIv);
        }

        //订单详情
        TextView orderDetailTv = helper.getView(R.id.order_activity_order_detail_tv);
        orderDetailTv.setOnClickListener(new OrderListener("order_detail"));

        //中间按钮
        TextView orderMiddleTv = helper.getView(R.id.order_activity_middle_tv);

        //右边按钮
        TextView orderRightTv = helper.getView(R.id.order_activity_right_tv);

        //若是最后一个条目，则删除底部线条
        View bottomLineView = helper.getView(R.id.order_activity_bottom_view);
        if (helper.getLayoutPosition() == shopBeanList.size() - 1) {

            bottomLineView.setVisibility(View.GONE);
        } else {
            bottomLineView.setVisibility(View.VISIBLE);
        }

        //判断订单状态
        //0.全部 1.待付款 2.待发货 3.待收货 4.待评价 4.已退款
        TextView orderStateTv = helper.getView(R.id.order_activity_order_state_tv);
        if (0 == orderState) {
            //全部
            orderStateTv.setText("全部");
        } else if (1 == orderState) {
            //待付款
            orderStateTv.setText("待付款");
            orderMiddleTv.setVisibility(View.GONE);
            orderRightTv.setText("申请退款");
            orderRightTv.setOnClickListener(new OrderListener("refund_money"));
        } else if (2 == orderState) {
            //待发货
            orderStateTv.setText("代发货");
            orderMiddleTv.setVisibility(View.GONE);
            orderRightTv.setText("再来一单");
        } else if (3 == orderState) {
            //待收货
            orderStateTv.setText("待收货");
            orderMiddleTv.setText("再来一单");
            orderRightTv.setVisibility(View.VISIBLE);
            orderRightTv.setText("立即评价");
        } else if (4 == orderState) {
            //待评价
            orderStateTv.setText("已完成");
            orderMiddleTv.setVisibility(View.GONE);
            orderRightTv.setText("查看退款");
        }
        //设置商品总数
        TextView goodsTotalNum = helper.getView(R.id.order_activity_total_num_tv);
        goodsTotalNum.setText("共" + goodsDetailsListBeans.size() + "件商品");
    }

    /**
     * 处理点击事件
     */
    class OrderListener implements View.OnClickListener {

        private String type;

        OrderListener(String type) {
            this.type = type;
        }

        @Override
        public void onClick(View view) {
            switch (type) {
                case "order_detail":
                    SkipUtils.toOrderDetail(mContext, orderState);
                    break;
                case "refund_money":
                    //申请退款

                    goPayBackWindow = new GoPayBackWindow(mContext, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            goPayBackWindow.dismiss();
                            goPayBackWindow = new GoPayBackWindow(mContext, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    Log.e("getResion()", goPayBackWindow.getResion().length() + "");
                                    if (TextUtils.isEmpty(goPayBackWindow.getResion()) || goPayBackWindow.getResion().length() <= 1) {

                                        Toast.makeText(mContext, "请先填写或输入原因", Toast.LENGTH_SHORT).show();

                                    } else {
                                        //refund(bean, goPayBackWindow.getResion());
                                        //todo 可在这里处理重复点击退款的问题

                                        goPayBackWindow.dismiss();
                                    }
                                }
                            });

                        }
                    });
                    MyOrderActivity myOrderActivity = weak.get();
                    goPayBackWindow.showAtLocation(myOrderActivity.getMainView(), Gravity.CENTER, 0, 0);
                    break;
            }
        }
    }
}
